package com.toman.templateActivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.request.PostRequest;
import com.toman.Enum.BaseEnum;
import com.toman.Enum.NetworkEnum;
import com.toman.Enum.TimeEnum;
import com.toman.Enum.ViewTypeEnum;
import com.toman.Interfaces.NewBaseUrl;
import com.toman.R;
import com.toman.Util.JsonUtils;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.bean.TemplateInfBean;
import com.toman.camera.Camera;
import com.toman.templateActivity.controler.TempFoldAdapter;
import com.toman.templateActivity.controler.TempInfAdapter;
import com.toman.toolbar.ToolBarActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by 邹苏隆 on 2018/11/5.
 * description：
 */
public class TempFoldActivity extends ToolBarActivity {
    @BindView(R.id.ll)
    LinearLayout ll;
    private TempInfAdapter adapter;
    private TempFoldAdapter foldAdapter;

    List<TemplateInfBean> list = new ArrayList<>();

    Camera camera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_problem_feedback_inf);
        setContentView(R.layout.layout_template_inf);
        initToolBar(BaseEnum.FEEDBACK.detail());

        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        list.add(new TemplateInfBean("departmentId", "部门名称", "", null, ViewTypeEnum.CLICK, NetworkEnum.DEP, mCompanyId, false, true,true));
        list.add(new TemplateInfBean("orderNum", "订单号", "", null, ViewTypeEnum.EDIT, InputType.TYPE_CLASS_TEXT, false, true,true));
        list.add(new TemplateInfBean("customer", "客户", "", null, ViewTypeEnum.EDIT,InputType.TYPE_CLASS_TEXT, false, true,true));
        list.add(new TemplateInfBean("provideDate", "最后交付日", "", null, ViewTypeEnum.DATE,TimeEnum.DATE, false, true,true ));
        list.add(new TemplateInfBean("demandCount", "净需求数", "", null, ViewTypeEnum.EDIT, InputType.TYPE_CLASS_NUMBER, false, true,true));
        list.add(new TemplateInfBean("productId", "品名", "", null, ViewTypeEnum.CLICK,NetworkEnum.PRODUCT, mCompanyId, false, true,true ));
        list.add(new TemplateInfBean("artlineId", "工艺路线名称", "", null, ViewTypeEnum.CLICK, NetworkEnum.ARTLINE, mCompanyId, false, true,true));
        list.add(new TemplateInfBean("remark", "备注", "", null, ViewTypeEnum.EDITMORE, true, true,false));


        foldAdapter=new TempFoldAdapter(list,"ok",true,false);
         TempFoldAdapter foldAdapter2=new TempFoldAdapter(list,"ok2",true);

        adapter = new TempInfAdapter(this,ll, Arrays.asList(foldAdapter, foldAdapter2));
adapter.addList(list);

adapter.addAdapters( Arrays.asList(foldAdapter));



//        camera = list.get(6).getCamera();

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        camera.onActivityResult(requestCode, resultCode, data);
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.toolbar_edit, menu);
//
//
//        MenuItem menuItem = menu.findItem(R.id.menu_edit);
////        还没配置权限
////        if (status.equals("3")|| !SPUtils.contains(getApplicationContext(), BaseEnum.FEEDBACK.has(PermEnum.EDIT))) {
////            menuItem.setVisible(false);
////        }
//        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
//
//            @Override
//            public boolean onMenuItemClick(MenuItem item) {
//                switch (item.getItemId()) {
//                    case R.id.menu_edit:
////                        if (item.getTitle().equals("编辑")) {
////                            item.setTitle("完成");
////                            adapter.edit(true);
////                            camera = list.get(6).getCamera();
////                        } else {
////                            item.setTitle("编辑");
////                            adapter.edit(false);
////                            camera = list.get(6).getCamera();
////
////                        }
//                        break;
//                }
//                return false;
//            }
//        });
//
//        return true;
//    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.toolbar_finish, menu);


        MenuItem menuItem = menu.findItem(R.id.menu_finish);
//        还没配置权限
//        if (status.equals("3")|| !SPUtils.contains(getApplicationContext(), BaseEnum.FEEDBACK.has(PermEnum.EDIT))) {
//            menuItem.setVisible(false);
//        }
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_finish:
//                        if (item.getTitle().equals("编辑")) {
//                            item.setTitle("完成");
//                            adapter.edit(true);
//                            camera = list.get(6).getCamera();
//                        } else {
//                            item.setTitle("编辑");
//                            adapter.edit(false);
//                            camera = list.get(6).getCamera();
//
//                        }
                        if (adapter.isEdit()){
                            add();
                        }
                        adapter.refresh(true);

//                        add();
                        break;
                }
                return false;
            }
        });

        return true;
    }

    public void add() {

        if (dialog != null) {
            dialog.dismiss();
        }
       if (adapter.isNull()){return;};
        Map<String, Object> map = new HashMap<String, Object>();

        for (TemplateInfBean bean :adapter.getAllList()){
            if (bean.getName()!=null&&bean.getType()!=ViewTypeEnum.PHOTO){
                map.put(bean.getName(),bean.getResult());
            }
        }
        String str = JsonUtils.obj2JsonString(map);

        PostRequest request = OkHttpUtils
                .post(NewBaseUrl.workshop_plan_save)
                .headers("token", this.token)
                .postJson(str);



              request  .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", "获取wifi: " + result);

                        JSONObject jsonObj = JSON.parseObject(result);
                        int code = jsonObj.getInteger("code");ViewUtil.lagout(code,getApplicationContext());

                        if (code == 0) {
                            ToastUtil.makeText(getApplicationContext(), jsonObj.getString("msg"));
//                            ToastUtil.makeText(getApplicationContext(),"获取wifi信号强度成功，状态约30s后会更新");
                            finish();


                        } else {
                            ToastUtil.error(getApplicationContext(), jsonObj.getString("msg"));
                        }

                    }
                });
    }


}
